<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<head>
    <title>Penlight Documentation</title>
    <link rel="stylesheet" href="../ldoc_fixed.css" type="text/css" />
</head>
<body>

<div id="container">

<div id="product">
	<div id="product_logo"></div>
	<div id="product_name"><big><b></b></big></div>
	<div id="product_description"></div>
</div> <!-- id="product" -->


<div id="main">


<!-- Menu -->

<div id="navigation">
<br/>
<h1>Penlight</h1>

<ul>
  <li><a href="https://github.com/lunarmodules/Penlight">GitHub Project</a></li>
  <li><a href="../index.html">Documentation</a></li>
</ul>

<h2>Contents</h2>
<ul>
<li><a href="#Creating_and_Displaying_Dates">Creating and Displaying Dates </a></li>
</ul>


<h2>Manual</h2>
<ul class="nowrap">
  <li><a href="../manual/01-introduction.md.html">Introduction</a></li>
  <li><a href="../manual/02-arrays.md.html">Tables and Arrays</a></li>
  <li><a href="../manual/03-strings.md.html">Strings. Higher-level operations on strings.</a></li>
  <li><a href="../manual/04-paths.md.html">Paths and Directories</a></li>
  <li><strong>Date and Time</strong></li>
  <li><a href="../manual/06-data.md.html">Data</a></li>
  <li><a href="../manual/07-functional.md.html">Functional Programming</a></li>
  <li><a href="../manual/08-additional.md.html">Additional Libraries</a></li>
  <li><a href="../manual/09-discussion.md.html">Technical Choices</a></li>
</ul>
<h2>Libraries</h2>
<ul class="nowrap">
  <li><a href="../libraries/pl.html">pl</a></li>
  <li><a href="../libraries/pl.app.html">pl.app</a></li>
  <li><a href="../libraries/pl.array2d.html">pl.array2d</a></li>
  <li><a href="../libraries/pl.class.html">pl.class</a></li>
  <li><a href="../libraries/pl.compat.html">pl.compat</a></li>
  <li><a href="../libraries/pl.comprehension.html">pl.comprehension</a></li>
  <li><a href="../libraries/pl.config.html">pl.config</a></li>
  <li><a href="../libraries/pl.data.html">pl.data</a></li>
  <li><a href="../libraries/pl.dir.html">pl.dir</a></li>
  <li><a href="../libraries/pl.file.html">pl.file</a></li>
  <li><a href="../libraries/pl.func.html">pl.func</a></li>
  <li><a href="../libraries/pl.import_into.html">pl.import_into</a></li>
  <li><a href="../libraries/pl.input.html">pl.input</a></li>
  <li><a href="../libraries/pl.lapp.html">pl.lapp</a></li>
  <li><a href="../libraries/pl.lexer.html">pl.lexer</a></li>
  <li><a href="../libraries/pl.luabalanced.html">pl.luabalanced</a></li>
  <li><a href="../libraries/pl.operator.html">pl.operator</a></li>
  <li><a href="../libraries/pl.path.html">pl.path</a></li>
  <li><a href="../libraries/pl.permute.html">pl.permute</a></li>
  <li><a href="../libraries/pl.pretty.html">pl.pretty</a></li>
  <li><a href="../libraries/pl.seq.html">pl.seq</a></li>
  <li><a href="../libraries/pl.sip.html">pl.sip</a></li>
  <li><a href="../libraries/pl.strict.html">pl.strict</a></li>
  <li><a href="../libraries/pl.stringio.html">pl.stringio</a></li>
  <li><a href="../libraries/pl.stringx.html">pl.stringx</a></li>
  <li><a href="../libraries/pl.tablex.html">pl.tablex</a></li>
  <li><a href="../libraries/pl.template.html">pl.template</a></li>
  <li><a href="../libraries/pl.test.html">pl.test</a></li>
  <li><a href="../libraries/pl.text.html">pl.text</a></li>
  <li><a href="../libraries/pl.types.html">pl.types</a></li>
  <li><a href="../libraries/pl.url.html">pl.url</a></li>
  <li><a href="../libraries/pl.utils.html">pl.utils</a></li>
  <li><a href="../libraries/pl.xml.html">pl.xml</a></li>
</ul>
<h2>Classes</h2>
<ul class="nowrap">
  <li><a href="../classes/pl.Date.html">pl.Date</a></li>
  <li><a href="../classes/pl.List.html">pl.List</a></li>
  <li><a href="../classes/pl.Map.html">pl.Map</a></li>
  <li><a href="../classes/pl.MultiMap.html">pl.MultiMap</a></li>
  <li><a href="../classes/pl.OrderedMap.html">pl.OrderedMap</a></li>
  <li><a href="../classes/pl.Set.html">pl.Set</a></li>
</ul>
<h2>Examples</h2>
<ul class="nowrap">
  <li><a href="../examples/seesubst.lua.html">seesubst.lua</a></li>
  <li><a href="../examples/sipscan.lua.html">sipscan.lua</a></li>
  <li><a href="../examples/symbols.lua.html">symbols.lua</a></li>
  <li><a href="../examples/test-cmp.lua.html">test-cmp.lua</a></li>
  <li><a href="../examples/test-data.lua.html">test-data.lua</a></li>
  <li><a href="../examples/test-listcallbacks.lua.html">test-listcallbacks.lua</a></li>
  <li><a href="../examples/test-pretty.lua.html">test-pretty.lua</a></li>
  <li><a href="../examples/test-symbols.lua.html">test-symbols.lua</a></li>
  <li><a href="../examples/testclone.lua.html">testclone.lua</a></li>
  <li><a href="../examples/testconfig.lua.html">testconfig.lua</a></li>
  <li><a href="../examples/testglobal.lua.html">testglobal.lua</a></li>
  <li><a href="../examples/testinputfields.lua.html">testinputfields.lua</a></li>
  <li><a href="../examples/testinputfields2.lua.html">testinputfields2.lua</a></li>
  <li><a href="../examples/testxml.lua.html">testxml.lua</a></li>
  <li><a href="../examples/which.lua.html">which.lua</a></li>
</ul>

</div>

<div id="content">


<h2>Date and Time</h2>

<p><a id="date"></a></p>

<p>NOTE: the Date module is deprecated</p>

<p><a name="Creating_and_Displaying_Dates"></a></p>
<h3>Creating and Displaying Dates</h3>

<p>The <a href="../classes/pl.Date.html#">Date</a> class provides a simplified way to work with <a href="http://www.lua.org/pil/22.1.html">date and
time</a> in Lua; it leans heavily on the functions
<a href="https://www.lua.org/manual/5.4/manual.html#pdf-os.date">os.date</a> and <a href="https://www.lua.org/manual/5.4/manual.html#pdf-os.time">os.time</a>.</p>

<p>A <a href="../classes/pl.Date.html#">Date</a> object can be constructed from a table, just like with <a href="https://www.lua.org/manual/5.4/manual.html#pdf-os.time">os.time</a>.
Methods are provided to get and set the various parts of the date.</p>


<pre>
&gt; d = <span class="function-name">Date</span> {year = <span class="number">2011</span>, month = <span class="number">3</span>, day = <span class="number">2</span> }
&gt; = d
<span class="number">2011</span>-<span class="number">03</span>-<span class="number">02</span> <span class="number">12</span>:<span class="number">00</span>:<span class="number">00</span>
&gt; = d:<span class="function-name">month</span>(),d:<span class="function-name">year</span>(),d:<span class="function-name">day</span>()
<span class="number">3</span>    <span class="number">2011</span>    <span class="number">2</span>
&gt; d:<span class="function-name">month</span>(<span class="number">4</span>)
&gt; = d
<span class="number">2011</span>-<span class="number">04</span>-<span class="number">02</span> <span class="number">12</span>:<span class="number">00</span>:<span class="number">00</span>
&gt; d:<span class="function-name">add</span> {day=<span class="number">1</span>}
&gt; = d
<span class="number">2011</span>-<span class="number">04</span>-<span class="number">03</span> <span class="number">12</span>:<span class="number">00</span>:<span class="number">00</span>
</pre>

<p><code>add</code> takes a table containing one of the date table fields.</p>


<pre>
&gt; = d:<span class="function-name">weekday_name</span>()
Sun
&gt; = d:<span class="function-name">last_day</span>()
<span class="number">2011</span>-<span class="number">04</span>-<span class="number">30</span> <span class="number">12</span>:<span class="number">00</span>:<span class="number">00</span>
&gt; = d:<span class="function-name">month_name</span>(<span class="keyword">true</span>)
April
</pre>

<p>There is a default conversion to text for date objects, but <a href="../classes/pl.Date.html#Date.Format">Date.Format</a> gives
you full control of the format for both parsing and displaying dates:</p>


<pre>
&gt; iso = Date.<span class="function-name">Format</span> <span class="string">'yyyy-mm-dd'</span>
&gt; d = iso:<span class="function-name">parse</span> <span class="string">'2010-04-10'</span>
&gt; amer = Date.<span class="function-name">Format</span> <span class="string">'mm/dd/yyyy'</span>
&gt; = amer:<span class="global">tostring</span>(d)
<span class="number">04</span>/<span class="number">10</span>/<span class="number">2010</span>
</pre>

<p>With the 0.9.7 release, the <a href="../classes/pl.Date.html#">Date</a> constructor has become more flexible. You may
omit any of the 'year', 'month' or 'day' fields:</p>


<pre>
&gt; = <span class="function-name">Date</span> { year = <span class="number">2008</span> }
<span class="number">2008</span>-<span class="number">01</span>-<span class="number">01</span> <span class="number">12</span>:<span class="number">00</span>:<span class="number">00</span>
&gt; = <span class="function-name">Date</span> { month = <span class="number">3</span> }
<span class="number">2011</span>-<span class="number">03</span>-<span class="number">01</span> <span class="number">12</span>:<span class="number">00</span>:<span class="number">00</span>
&gt; = <span class="function-name">Date</span> { day = <span class="number">20</span> }
<span class="number">2011</span>-<span class="number">10</span>-<span class="number">20</span> <span class="number">12</span>:<span class="number">00</span>:<span class="number">00</span>
&gt; = <span class="function-name">Date</span> { hour = <span class="number">14</span>, min = <span class="number">30</span> }
<span class="number">2011</span>-<span class="number">10</span>-<span class="number">13</span> <span class="number">14</span>:<span class="number">30</span>:<span class="number">00</span>
</pre>

<p>If 'year' is omitted, then the current year is assumed, and likewise for 'month'.</p>

<p>To set the time on such a partial date, you can use the fact that the 'setter'
methods return the date object and so you can 'chain' these methods.</p>


<pre>
&gt; d = <span class="function-name">Date</span> { day = <span class="number">03</span> }
&gt; = d:<span class="function-name">hour</span>(<span class="number">18</span>):<span class="function-name">min</span>(<span class="number">30</span>)
<span class="number">2011</span>-<span class="number">10</span>-<span class="number">03</span> <span class="number">18</span>:<span class="number">30</span>:<span class="number">00</span>
</pre>

<p>Finally, <a href="../classes/pl.Date.html#">Date</a> also now accepts positional arguments:</p>


<pre>
&gt; = <span class="function-name">Date</span>(<span class="number">2011</span>,<span class="number">10</span>,<span class="number">3</span>)
<span class="number">2011</span>-<span class="number">10</span>-<span class="number">03</span> <span class="number">12</span>:<span class="number">00</span>:<span class="number">00</span>
&gt; = <span class="function-name">Date</span>(<span class="number">2011</span>,<span class="number">10</span>,<span class="number">3</span>,<span class="number">18</span>,<span class="number">30</span>,<span class="number">23</span>)
<span class="number">2011</span>-<span class="number">10</span>-<span class="number">03</span> <span class="number">18</span>:<span class="number">30</span>:<span class="number">23</span>
</pre>

<p><code>Date.format</code> has been extended. If you construct an instance without a pattern,
then it will try to match against a set of known formats. This is useful for
human-input dates since keeping to a strict format is not one of the strong
points of users. It assumes that there will be a date, and then a date.</p>


<pre>
&gt; df = Date.<span class="function-name">Format</span>()
&gt; = df:<span class="function-name">parse</span> <span class="string">'5.30pm'</span>
<span class="number">2011</span>-<span class="number">10</span>-<span class="number">13</span> <span class="number">17</span>:<span class="number">30</span>:<span class="number">00</span>
&gt; = df:<span class="function-name">parse</span> <span class="string">'1730'</span>
<span class="keyword">nil</span>     day out of range: <span class="number">1730</span> is <span class="keyword">not</span> between <span class="number">1</span> <span class="keyword">and</span> <span class="number">31</span>
&gt; = df:<span class="function-name">parse</span> <span class="string">'17.30'</span>
<span class="number">2011</span>-<span class="number">10</span>-<span class="number">13</span> <span class="number">17</span>:<span class="number">30</span>:<span class="number">00</span>
&gt; = df:<span class="function-name">parse</span> <span class="string">'mar'</span>
<span class="number">2011</span>-<span class="number">03</span>-<span class="number">01</span> <span class="number">12</span>:<span class="number">00</span>:<span class="number">00</span>
&gt; = df:<span class="function-name">parse</span> <span class="string">'3 March'</span>
<span class="number">2011</span>-<span class="number">03</span>-<span class="number">03</span> <span class="number">12</span>:<span class="number">00</span>:<span class="number">00</span>
&gt; = df:<span class="function-name">parse</span> <span class="string">'15 March'</span>
<span class="number">2011</span>-<span class="number">03</span>-<span class="number">15</span> <span class="number">12</span>:<span class="number">00</span>:<span class="number">00</span>
&gt; = df:<span class="function-name">parse</span> <span class="string">'15 March 2008'</span>
<span class="number">2008</span>-<span class="number">03</span>-<span class="number">15</span> <span class="number">12</span>:<span class="number">00</span>:<span class="number">00</span>
&gt; = df:<span class="function-name">parse</span> <span class="string">'15 March 2008 1.30pm'</span>
<span class="number">2008</span>-<span class="number">03</span>-<span class="number">15</span> <span class="number">13</span>:<span class="number">30</span>:<span class="number">00</span>
&gt; = df:<span class="function-name">parse</span> <span class="string">'2008-10-03 15:30:23'</span>
<span class="number">2008</span>-<span class="number">10</span>-<span class="number">03</span> <span class="number">15</span>:<span class="number">30</span>:<span class="number">23</span>
</pre>

<p>ISO date format is of course a good idea if you need to deal with users from
different countries. Here is the default behaviour for 'short' dates:</p>


<pre>
&gt; = df:<span class="function-name">parse</span> <span class="string">'24/02/12'</span>
<span class="number">2012</span>-<span class="number">02</span>-<span class="number">24</span> <span class="number">12</span>:<span class="number">00</span>:<span class="number">00</span>
</pre>

<p>That's not what Americans expect! It's tricky to work out in a cross-platform way
exactly what the expected format is, so there is an explicit flag:</p>


<pre>
&gt; df:<span class="function-name">US_order</span>(<span class="keyword">true</span>)
&gt; = df:<span class="function-name">parse</span> <span class="string">'9/11/01'</span>
<span class="number">2001</span>-<span class="number">11</span>-<span class="number">09</span> <span class="number">12</span>:<span class="number">00</span>:<span class="number">00</span>
</pre>



</div> <!-- id="content" -->
</div> <!-- id="main" -->
<div id="about">
<i>generated by <a href="http://github.com/stevedonovan/LDoc">LDoc 1.5.0</a></i>
</div> <!-- id="about" -->
</div> <!-- id="container" -->
</body>
</html>
